﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using BSCMFF.Core.BusinessLayer;
using BSCMFF.Core.DataAccessLayer;

namespace BSCMFF.Web.UserControls.Selections
{
    public partial class EmployeeSimpleSelection : System.Web.UI.UserControl
    {
        public bool PublicAutoPostback
        {
            get
            {
                return RadComboBoxEmployee.AutoPostBack;
            }
            set
            {
                RadComboBoxEmployee.AutoPostBack = value;
            }
        }

        private int? _selectedUserID;
        public int? SelectedUserID 
        { 
            get
            {
                if (RadComboBoxEmployee.SelectedValue == "")
                {
                    return null;
                }
                else
                {
                    return Int32.Parse(RadComboBoxEmployee.SelectedValue);// _selectedUserID;
                }
            }
            set
            {
                if (value != null)
                {
                    _selectedUserID = value;
                    //RadComboBoxEmployee.SelectedValue = value.ToString();
                }
                //else
                //{
                //    RadComboBoxEmployee.SelectedValue = "";
                //}
                //else
                //{
                //    return Int32.Parse(RadComboBoxEmployee.SelectedValue);// _selectedUserID;
                //}
                //_selectedUserID = value;
            }
        }


        public event EventHandler EventEmployeeChanged;
        //private List<User> allUsers;// = new List<User>();

        protected void Page_Load(object sender, EventArgs e)
        {
            //if (IsPostBack)
            //{

                PopulateEmployees(null);


                //}

                if (_selectedUserID != null)
                {
                    RadComboBoxEmployee.SelectedValue = _selectedUserID.ToString();// SelectedUserID.ToString();
                }
            //}
        }

        private void PopulateEmployees2(List<User> users)
        {
            RadComboBoxEmployee.DataSource = users;
            
            RadComboBoxEmployee.DataBind();
        }

        private void PopulateEmployees(int? SBU_ID)
        {
            UserManager um = new UserManager();

            if (SBU_ID == null)
            {
                RadComboBoxEmployee.DataSource = um.GetAllUsers();
            }
            else
            {
                RadComboBoxEmployee.DataSource = um.GetAllUsersByDepartment((int)SBU_ID);
            }
            RadComboBoxEmployee.DataBind();
            //RadComboBoxEmployee.Enabled = true;
        }

        

        public void RadComboBoxEmployee_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            if (EventEmployeeChanged != null)
            {
                EventEmployeeChanged(sender, e);
            }
        }

        public void SetSelectedEmployee(int? userID)
        {
            if (userID != null)
            {
                RadComboBoxEmployee.SelectedValue = SelectedUserID.ToString();
            }
        }

        public string more(object o)
        {
            string s = "";
            return s;
        }
    }
}